package com.manager.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manager.pojo.TbUser;
import com.manager.service.UserService;
import com.manager.vo.TbUserEx;

/**
 * 
 * @Title:  AdminController.java     
 * @Description:    管理员controller
 * @author:      
 * @date:   2019年2月14日 下午11:20:57   
 * @version V1.0
 */
@Controller
public class AdminControl {

	@Autowired
	private UserService userService;
	
	//密码修改
	@RequestMapping("/user-passwords")		
	@ResponseBody
	public TbUserEx userPasswords(String oldpassword,TbUser tbUser,HttpSession session) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbUser temptbUser = null;
		
		try {
			
			temptbUser = userService.getUserById(tbUser.getId());
			if(oldpassword.equalsIgnoreCase(temptbUser.getPassword())) {
				tbUserEx = userService.editUser(tbUser);
			}else {
				tbUserEx.setCode(400);
				tbUserEx.setError("你输入的旧密码错误");
				return tbUserEx;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("密码修改异常");
			return tbUserEx;
		}
		
		if(tbUser.getId()==1) {
			tbUserEx.setCode(200);
			tbUserEx.setMsg("密码修改成功,请重新登入");
			session.removeAttribute("user");
		}else {
			tbUserEx.setCode(200);
			tbUserEx.setMsg("密码修改成功");
		}
		
		return tbUserEx;
	}
	
	//通过ID 获取账号
	@RequestMapping("/user-password")		
	public String userPassword(Integer id,Model model) {
		
		TbUser tbUser = null;
		
		try {
			tbUser = userService.getUserById(id);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		model.addAttribute("tbUser", tbUser);
		
		return "user-password";
	}
}
